--[[
--ltui的使用

]]

local ltui        = require("ltui")

local UI = class("UI")

function UI:ctor( ... )

end

function UI:onStart( ... )
    --self:dialog()


    g.fork(function()
        self:test()
    end)
    print("111111@@@@@@@@")

end

--dialog
function UI:dialog( ... )
    local label       = ltui.label
    local button      = ltui.button
    local application = ltui.application
    local event       = ltui.event
    local rect        = ltui.rect
    local boxdialog   = ltui.boxdialog
    local textdialog  = ltui.textdialog
    local inputdialog = ltui.inputdialog

    -- the xlibdemo application
    local app = application()

    -- init app
    function app:init()
        -- init name
        application.init(self, "app")
        -- init background
        self:background_set("blue")
        -- init main dialog
        self:insert(self:dialog_main())
        -- init input dialog
        self:insert(self:dialog_input(), {centerx = true, centery = true})
        -- init tips dialog
        self:insert(self:dialog_tips(), {centerx = true, centery = true})
    end
    -- main dialog
    function app:dialog_main()
        local dialog_main = self._DIALOG_MAIN
        if not dialog_main then
            dialog_main = boxdialog:new("dialog.main", rect {1, 1, self:width() - 1, self:height() - 1}, "main dialog")
            dialog_main:text():text_set("The project focuses on making development and building easier and provides many features (.e.g package, \
            install, plugin, macro, action, option, task ...), so that any developer can quickly pick it \
             up and enjoy the productivity boost when developing and building project.")
            dialog_main:button_add("tips", "< Tips >", function (v) self:view("dialog.tips"):show(true, {focused = true}) end)
            dialog_main:button_add("input", "< Input >", function (v) self:view("dialog.input"):show(true, {focused = true}) end)
            dialog_main:button_add("help", "< Help >", function (v) self:insert(self:dialog_help()) end)
            dialog_main:button_add("quit", "< Quit >", "cm_quit")
            self._DIALOG_MAIN = dialog_main
        end
        return dialog_main
    end

    -- help dialog
    function app:dialog_help()
        local dialog_help = self._DIALOG_HELP
        if not dialog_help then
            dialog_help = textdialog:new("dialog.help", rect {1, 1, self:width() - 1, self:height() - 1}, "README")
            dialog_help:option_set("scrollable", true)
            local helptext = nil
            local src_path = g.getenv("root")
            local file = io.open(src_path.."common/services/test/ui_test.lua", 'r')
            if file then
                helptext = file:read("*a")
                file:close()
            end
            if helptext then
                dialog_help:text():text_set(helptext)
            end
            dialog_help:button_add("exit", "< Exit >", function (v) self:remove(dialog_help) end)
            self._DIALOG_HELP = dialog_help
        end
        return dialog_help
    end

    -- input dialog
    function app:dialog_input()
        local dialog_input = self._DIALOG_INPUT
        if not dialog_input then
            dialog_input = inputdialog:new("dialog.input", rect {0, 0, 50, 8}):background_set(self:dialog_main():frame():background())
            dialog_input:frame():background_set("cyan")
            dialog_input:text():text_set("please input text:"):textattr_set("red")
            dialog_input:button_add("no", "< No >", function (v) dialog_input:show(false) end)
            dialog_input:button_add("yes", "< Yes >", function (v)
                self:dialog_main():text():text_set(dialog_input:textedit():text())
                dialog_input:show(false)
            end)
            dialog_input:show(false)
            self._DIALOG_INPUT = dialog_input
        end
        return dialog_input
    end

    -- tips dialog
    function app:dialog_tips()
        local dialog_tips = self._DIALOG_TIPS
        if not dialog_tips then
            dialog_tips = textdialog:new("dialog.tips", rect {0, 0, 50, 8}):background_set(self:dialog_main():frame():background())
            dialog_tips:frame():background_set("cyan")
            dialog_tips:text():text_set("hello ltui! (https://tboox.org)\nA cross-platform terminal client library based on Lua"):textattr_set("red")
            dialog_tips:button_add("yes", "< Yes >", function (v) dialog_tips:show(false) end)
            dialog_tips:button_add("no", "< No >", function (v) dialog_tips:show(false) end)
            self._DIALOG_TIPS = dialog_tips
        end
        return dialog_tips
    end

    -- on resize
    function app:on_resize()
        self:dialog_main():bounds_set(rect {1, 1, self:width() - 1, self:height() - 1})
        self:dialog_help():bounds_set(rect {1, 1, self:width() - 1, self:height() - 1})
        self:center(self:dialog_input(), {centerx = true, centery = true})
        self:center(self:dialog_tips(), {centerx = true, centery = true})
        application.on_resize(self)
    end

    -- run app
    app:run()
end

function UI:test( ... )
    local label       = ltui.label
    local button      = ltui.button
    local application = ltui.application
    local event       = ltui.event
    local rect        = ltui.rect
    local boxdialog   = ltui.boxdialog
    local textdialog  = ltui.textdialog
    local inputdialog = ltui.inputdialog
    local panel = ltui.panel

    -- the xlibdemo application
    local app = application()
    local label1
    local dialog1
    -- init app
    function app:init()
        -- init name
        application.init(self, "app")
        -- init background
        self:background_set("blue")
        local panel1 = panel:new("panel1", self:bounds())
        self:insert(panel1)
        --panel1:insert(ltui.border:new("11", self:bounds()))
        ---- init main dialog
        --self:insert(self:dialog_main())
        ---- init input dialog
        local text = "text"
        local len = #text
        --self:insert(label:new("test", len, text), {centerx = true, centery = true})
        -- add title label


        local str = string.format("width:%d, height:%d, size:%s", self:width(), self:height(), self:size())
        label1 = label:new("title", rect({0, 0, #str, 1}), str):textattr_set("white")
        panel1:insert(label1)
        dialog1 = panel:new("dialog1", rect {0, 0, 1, 1})
        dialog1:background_set("red")
        panel1:insert(dialog1)

        --label1:bounds():move2(10, 5)
        dialog1:bounds():move2(10, 0)
    end

    function app:on_event(e)
        if e.type < event.ev_max then
            local str = 'type: ' ..
                    tostring(e.type) ..
                    '; name: ' ..
                    tostring(e.key_name or e.btn_name) ..
                    '; code: ' ..
                    tostring(e.key_code or e.x) ..
                    '; meta: ' ..
                    tostring(e.key_code or e.y)
            --label1:bounds_set(rect({10,0,#str,10}))
            label1:text_set(str)


            if e.key_code == 115 then
                local bounds = dialog1:bounds()
                bounds:move(0, 1)
                dialog1:invalidate(true)
            elseif e.key_code == 119 then
                local bounds = dialog1:bounds()
                bounds:move(0, -1)
                dialog1:invalidate(true)
            elseif e.key_code == 100 then
                local bounds = dialog1:bounds()
                bounds:move(1, 0)
                dialog1:invalidate(true)
            elseif e.key_code == 97 then
                local bounds = dialog1:bounds()
                dialog1:invalidate(true)
            end
        end
        application.on_event(self, e)
    end

    --
    function app:on_draw()
        application.on_draw(self)
    end

    -- run program loop
    function app:loop(argv)
        -- do message loop
        local e = nil
        local sleep = true
        g.fork(function()
            while true do
                -- get the current event
                e = self:event()
                -- do event
                if e then
                    event.dump(e)
                    self:on_event(e)
                    sleep = false
                else
                    -- do idle event
                    self:on_event(event.idle())
                    sleep = true
                end
                -- quit?
                if e and event.is_command(e, "cm_quit") then
                    break
                end
                -- resize views
                if self:state("resize") then
                    self:on_resize()
                end
                -- draw views
                self:on_draw()
                -- refresh views
                if self:state("refresh") then
                    self:on_refresh()
                end
                -- wait some time, 50ms
                if sleep then
                    g.sleep(50)
                end
            end
        end)
    end

    app:run()


    g.fork(function()
        while(true) do
            g.sleep(100)
            local bounds = dialog1:bounds()
            bounds:move(0, 1)
            dialog1:invalidate(true)
        end
    end)

end


return UI
